package com.denlaku.longan.controller.impl;

import cn.hutool.core.util.ZipUtil;
import com.denlaku.longan.anno.VarBy;
import com.denlaku.longan.config.LonganProperties;
import com.denlaku.longan.controller.CustomPageInfoController;
import com.denlaku.longan.core.Current;
import com.denlaku.longan.http.Resp;
import com.denlaku.longan.http.Return;
import com.denlaku.longan.qo.CustomPageQuery;
import com.denlaku.longan.qo.Query;
import com.denlaku.longan.service.CustomPageService;
import com.denlaku.longan.util.FileUtil;
import com.denlaku.longan.vo.CustomPage;
import com.denlaku.longan.vo.PageBy;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipInputStream;

/**
 * @author tianx
 */
@Slf4j
@RestController
public class CustomPageInfoControllerImpl implements CustomPageInfoController {
    @Autowired
    private CustomPageService customPageService;

    @Autowired
    private LonganProperties configProperties;

    @Override
    public Return<Long> add(@RequestBody CustomPage customPage) {
        customPageService.add(customPage);
        return Resp.success(customPage.getId());
    }

    @Override
    public Return<Void> update(@RequestBody CustomPage customPage) {
        customPageService.update(customPage);
        return Resp.success();
    }

    @Override
    public Return<Void> delete(Long id) {
        customPageService.delete(Query.of(id));
        return Resp.success();
    }

    @Override
    public Return<Void> upload(MultipartHttpServletRequest request) {
            Iterator<String> fileNames = request.getFileNames();
            if (!fileNames.hasNext()) {
                return Resp.error("没有选择文件！");
            }
            String fileName = fileNames.next();
            MultipartFile mFile = request.getFile(fileName);
            if (mFile == null) {
                return Resp.error("没有选择文件！");
            }
            String id = request.getParameter("id");
            CustomPage customPage = customPageService.get(Query.of(Long.parseLong(id)));
            if (customPage == null) {
                return Resp.error("页面不存在！");
            }
        String tenantId = String.valueOf(Current.getTenantId());
        Path path = Paths.get(configProperties.getResourceLocation(), "custom-page", tenantId, id);
            File descDir = path.toFile();
            if (descDir.exists()) {
                FileUtil.delete(descDir);
            }
            if (!descDir.exists() && !descDir.mkdirs()) {
                return Resp.error("创建目录失败！");
            }
        try {
            ZipInputStream zipInputStream = new ZipInputStream(mFile.getInputStream());
            ZipUtil.unzip(zipInputStream, descDir);
            File[] files = descDir.listFiles();
            String zipRoot = null;
            if (files != null && files.length == 1 && files[0].isDirectory()) {
                File dir = files[0];
                String path1 = dir.getPath();
                String path2 = descDir.getPath();
                zipRoot = path1.substring(path2.length() + 1);
                System.out.println(path1);
            }
            customPage.setZipRoot(zipRoot);
            customPageService.update(customPage);
            return Resp.message("上传成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return Resp.error("上传失败");
    }

    @VarBy
    @Override
    public Return<List<CustomPage>> pageList(@RequestBody CustomPageQuery query, PageBy pageBy) {
        PageHelper.startPage(pageBy.getPage(), pageBy.getSize());
        List<CustomPage> list = customPageService.list(query);
        return Resp.success(list);
    }

    @Override
    public Return<CustomPage> get(Long id) {
        CustomPage customPage = customPageService.get(Query.of(id));
        return Resp.success(customPage);
    }

}
